package cn.tedu.straw.api.question.controller;

import cn.tedu.straw.api.question.dto.PostQuestionDTO;
import cn.tedu.straw.api.question.ex.InvalidParameterException;
import cn.tedu.straw.api.question.service.IQuestionService;
import cn.tedu.straw.api.question.vo.QuestionDetailVO;
import cn.tedu.straw.api.question.vo.QuestionListItemVO;
import cn.tedu.straw.api.question.vo.QuestionSimpleVO;
import cn.tedu.straw.commons.security.LoginUserInfo;
import cn.tedu.straw.commons.util.R;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/portal/question")
@Slf4j
public class QuestionController {

    @Autowired
    IQuestionService questionService;

    // http://localhost/api-question/portal/question/post?title=TestTitle2&content=TestContent2&tagIds=1&tagIds=3&tagIds=15&teacherIds=2&teacherIds=4&teacherIds=5
    @RequestMapping("/post")
    public R<Void> post(@Valid PostQuestionDTO postQuestionDTO, BindingResult bindingResult,
                        @AuthenticationPrincipal LoginUserInfo loginUserInfo) {
        log.debug("发布问题，客户端提交的数据：{}", postQuestionDTO);
        log.debug("发布问题，当前登录的用户信息：{}", loginUserInfo);

        if (bindingResult.hasErrors()) {
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new InvalidParameterException(errorMessage);
        }

        Integer userId = loginUserInfo.getUserId();
        String userNickName = loginUserInfo.getNickName();
        questionService.post(postQuestionDTO, userId, userNickName);
        return R.ok();
    }

    // http://localhost/api-question/portal/question/most-hits
    @GetMapping("/most-hits")
    public R<List<QuestionSimpleVO>> getMostHitsList() {
        return R.ok(questionService.getMostHitsList());
    }

    // http://localhost/api-question/portal/question/my?page=1
    @GetMapping("/my")
    public R<PageInfo<QuestionListItemVO>> getMyQuestions(Integer page, @AuthenticationPrincipal LoginUserInfo loginUserInfo) {
        Integer userId = loginUserInfo.getUserId();
        PageInfo<QuestionListItemVO> pageInfo = questionService.getMyQuestions(userId, page);
        return R.ok(pageInfo);
    }

    // http://localhost/api-question/portal/question/100
    @GetMapping("/{questionId}")
    public R<QuestionDetailVO> getDetail(@PathVariable("questionId") Integer questionId) {
        return R.ok(questionService.getDetail(questionId));
    }

}